﻿using Authentication.Model.Interface;
using Common.Contract.Authentication.Record;
using Common.Contract.Authentication.Request;
using Common.Contract.Authentication.Response;
using Common.Service.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Authentication.Core.ServiceCommand
{
    public class RetrieveUserByDaoServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveUserByDaoRequest, RetrieveUserResponse>
    {
        #region Properties
        public UserRecord Dao;
        public string DaoNeedFind;
        #endregion
        public RetrieveUserByDaoServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveUserByDaoRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.CurrentDao == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = request.CurrentDao;
            }
            if (string.IsNullOrEmpty(Request.Dao))
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                DaoNeedFind = request.Dao;
            }
        }

        protected override bool OnExecute(RetrieveUserResponse response)
        {
            bool isSuccess = true;
            try
            {

                var user = Providers.Users.RetrieveUserByDao(DaoNeedFind, Dao);
                if (user == null)
                {
                    response.ErrorMessage = string.Format("Can not found user with Dao={0}", Request.Dao);
                    response.IsSuccess = false;
                }
                UserRecord record = new UserRecord
                {
                    UserName = user.UserName,
                    Dao = user.Dao,
                    ContractType = user.PositionName,
                    Branch = user.Branch == null ? string.Empty : user.Branch.BranchNameSme,
                    StartWorkingDate = user.DateSalesStart.HasValue ? user.DateSalesStart.Value.ToShortDateString() : DateTime.Now.ToShortDateString(),
                    Zone = user.Branch == null ? string.Empty : user.Branch.Region,
                    BranchCode = user.BranchCodeSme,
                    BranchId = user.Branch == null ? string.Empty : user.Branch.BranchId,
                    Email = user.Email,
                    FullName = user.DaoName
                };
                response.Record = record;

            }
            catch (Exception e)
            {
                Console.Write(e.Message);
                response.ErrorMessage = string.Format("Can not found user with Dao={0}", Request.Dao);
                response.IsSuccess = false;
            }
            return isSuccess;
        }
    }
}
